package com.ZeroCarbon.controller;

import cn.hutool.core.util.BooleanUtil;
import com.ZeroCarbon.domain.dto.GoodsReceiverDTO;
import com.ZeroCarbon.domain.dto.IdentityReceiverDTO;
import com.ZeroCarbon.domain.po.Address;
import com.ZeroCarbon.domain.po.Comment;
import com.ZeroCarbon.domain.po.Order;
import com.ZeroCarbon.entity.Result;
import com.ZeroCarbon.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 商城信息控制器
 *
 * @since 2024-11-7
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/shop")
public class ShopController {

    private final IIdentityService identityService;

    private final IGoodsService goodsService;

    private final ICommentService commentService;

    private final IAddressService addressService;

    private final ICartService cartService;

    private final IOrderService orderService;

    /**
     * 保存身份
     *
     * @param identity 身份信息
     */
    @PostMapping("/addIdentity")
    public Result addIdentity(IdentityReceiverDTO identity) {
        try {
            identityService.addIdentity(identity);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取当前用户的商店
     */
    @GetMapping("/getIdentity")
    public Result getIdentity() {
//        try {
//        } catch (Exception e) {
//
//            return Result.error(e.getMessage());
//        }
        return Result.success(identityService.getIdentities());
    }

    /**
     * 获取当前用户商店简略信息
     */
    @GetMapping("/getIdentityPreview")
    public Result getIdentityPreview() {
        try {
            return Result.success(identityService.getIdentitiesPreview());
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 根据id获取商店
     *
     * @param id 商店id
     */
    @GetMapping("/getIdentityById")
    public Result getIdentityById(@RequestParam String id) {
        try {
            return Result.success(identityService.getIdentity(id));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新商店信息
     *
     * @param identityReceiverDTO 具体身份信息
     */
    @PostMapping("/updateIdentity")
    public Result updateIdentity(IdentityReceiverDTO identityReceiverDTO) {
        try {
            identityService.updateIdentity(identityReceiverDTO);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除商店信息
     *
     * @param id 商店id
     */
    @GetMapping("/deleteIdentity")
    public Result deleteIdentity(@RequestParam String id) {
        try {
            identityService.deleteIdentity(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 新增商品
     */
    @PostMapping("/addGood")
    public Result addGood(GoodsReceiverDTO goodsReceiverDTO) {
        try {
            goodsService.addGood(goodsReceiverDTO);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取当前用户商品
     */
    @GetMapping("/getGoods")
    public Result getGoods(@RequestParam(defaultValue = "1") int page,
                           @RequestParam(defaultValue = "6") int size,
                           @RequestParam boolean isSave) {
        try {
            return Result.success(goodsService.getGoodsList(page, size, isSave, false));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 主页随机获取商品
     */
    @GetMapping("/getGoodsRandom")
    public Result getGoodsRandom(@RequestParam(defaultValue = "1") int page,
                                 @RequestParam(defaultValue = "6") int size) {
        try {
            return Result.success(goodsService.getGoodsList(page, size, false, true));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取商品详细信息
     */
    @GetMapping("/getGoodDetail")
    public Result getGoodDetail(@RequestParam String id) {
        try {
            return Result.success(goodsService.getGoodDetail(id));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 用户发表评论
     *
     * @param comment 评论内容
     */
    @PostMapping("/postComment")
    public Result postComment(@RequestBody Comment comment) {
        try {
            commentService.postComment(comment);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取商品评论
     */
    @GetMapping("/getComments")
    public Result getComments(@RequestParam String id) {
        try {
            return Result.success(commentService.getComments(id));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 点赞评论
     *
     * @param id 评论id
     */
    @GetMapping("/thumbsUp")
    public Result thumbsUp(@RequestParam String id) {
        try {
            int thumb = commentService.thumbsUpComment(id);
            //获取点赞数
            return Result.success(thumb);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除评论
     *
     * @param id 评论id
     */
    @GetMapping("/deleteGoodComment")
    public Result deleteGoodComment(String id) {
        try {
            commentService.deleteComment(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 判断当前用户是否对记录点赞
     *
     * @param id 评论id
     */
    @GetMapping("/isUserLikeShopComment")
    public Result isUserLikeComment(String id) {
        try {
            boolean isLike = BooleanUtil.isTrue(commentService.isUserLikeShopComment(id));
            return Result.success(isLike);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取当前用户所有地址
     */
    @GetMapping("/getAllAddress")
    public Result getAllAddress() {
        try {
            return Result.success(addressService.getAllAddress());
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 新增收货地址
     */
    @PostMapping("/addNewAddress")
    public Result addNewAddress(@RequestBody Address address) {
        try {
            addressService.addNewAddress(address);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取当前账号的地址
     */
    @GetMapping("/getCurrentAddress")
    public Result getCurrentAddress() {
        try {
            return Result.success(addressService.getCurrentAddress());
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新收货地址
     */
    @PostMapping("/updateAddress")
    public Result updateAddress(@RequestBody Address address) {
        try {
            addressService.updateAddress(address);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 加入购物车
     *
     * @param id 商品id
     */
    @GetMapping("/addGoodToCart")
    public Result addGoodToCart(@RequestParam String id) {
        try {
            cartService.addGoodToCart(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 从购物车中删除
     */
    @GetMapping("/deleteGoodFromCart")
    public Result deleteGoodFromCart(@RequestParam String id) {
        try {
            cartService.deleteGoodFromCart(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取购物车信息
     */
    @GetMapping("/getGoodsFromCart")
    public Result getGoodsFromCart() {
        try {
            return Result.success(cartService.getGoodsFromCart());
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加订单
     */
    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody Order order) {
        try {
            orderService.saveOrder(order);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取当前用户所有订单
     */
    @GetMapping("/getOrder")
    public Result getOrder() {
        try {
            return Result.success(orderService.getOrder());
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 搜索商品
     *
     * @param search 搜索内容
     * @param page   页数
     * @param size   页长
     */
    @GetMapping("/searchGood")
    public Result searchGood(@RequestParam String search,
                             @RequestParam(defaultValue = "1") int page,
                             @RequestParam(defaultValue = "999") int size) {
        try {
            return Result.success(goodsService.searchGood(search, page, size));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
